package demo.practice.dp;

import java.util.Arrays;

public class P128 {

    public static void main(String[] args) {
        P128 p128 = new P128();
        p128.longestConsecutive(new int[]{1, 2, 0, 1});
    }


        public int longestConsecutive(int[] nums) {
            if (nums == null || nums.length == 0) {
                return 0;
            }
            Arrays.sort(nums);
            int max = 1;
            int nu = 1;
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == nums[i - 1] + 1) {
                    nu++;
                    //max=Math.max(nu,)
                } else if (nums[i] == nums[i - 1]) {
                    continue;
                } else {
                    max = Math.max(nu, max);
                    nu = 1;
                }
            }
            return Math.max(nu, max);
        }


    /*public int longestConsecutive(int[] nums) {

        TreeSet<Integer> set = new TreeSet<>();
        for (int num : nums) {
            set.add(num);
        }

        Integer before = null;
        int nu = 1;
        int max = 1;
        for (Integer s : set) {
            if (before == null) {
                before = s;
                continue;
            }

            if (s == before + 1) {
                nu++;
                //max=Math.max(nu,)
            } else {
                max = Math.max(nu, max);
                nu = 1;
            }

            before = s;

        }


        return Math.max(nu, max);

    }*/
}
